package task

import (
	"context"
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/zcamp-service-user/app/config"
	"gitee.com/zaiqiang231/zcamp-service-user/app/store"
	"time"
)

var batchNum int = 1000

func UidGen() {
	ctx := context.Background()
	sz, err := store.UserIdMqLen(ctx)
	if err != nil {
		baseLog.Errorf("redis get UserIdMqLen error (%v)", err)
	}

	if sz <= int64(config.GlobalAppConfig.IdGenFactor*float64(config.GlobalAppConfig.IdMqBaseSize)) {
		num := 0
		for {
			_, err := store.UserIdGenToMQ(ctx)
			if err != nil {
				baseLog.Errorf("UserIdGenToMQ error (%v)", err)
				break
			}
			num++
			if num < batchNum {
				continue
			}
			sz, err = store.UserIdMqLen(ctx)
			if err != nil {
				baseLog.Errorf("redis get userIdMqLen error (%v)", err)
				break
			}

			if sz > config.GlobalAppConfig.IdMqBaseSize {
				baseLog.Infof("redis userIdMqLen %v is great than %v", sz, config.GlobalAppConfig.IdMqBaseSize)
				break
			}
			time.Sleep(time.Millisecond * 100)
			num = 0
		}
	}

}
